<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>恒铸智能柜管理平台</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/resources/layui/css/layui.css"
	media="all" />
<script type="text/javascript"
	src="${pageContext.request.contextPath}/resources/layui/layui.js"></script>
</head>
<style>
.laytable-cell-1-url {
	width: 340px !important;
}

.layui-form-item {
	display: inline-block;
	margin-bottom: 0px;
}
</style>
<body class="childrenBody">
	<form class="layui-form">
		<blockquote class="layui-elem-quote admin_search">
			<div>
				<a class="layui-btn addbtn" id="addbtn">新增</a>
			</div>
		</blockquote>

		<input type="hidden" id="compId" value="${compId}">
		<div class="layui-form-item selectComp">
			<label class="layui-form-label">公司:</label>
			<div class="layui-input-block">
				<select name="interest" lay-filter="selectComp">
					<option value="">全部</option>
					<c:forEach items="${comps }" var="list">
						<option value="${list.compId }">${list.compName }</option>
					</c:forEach>
				</select>
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">类型:</label>
			<div class="layui-input-block">
				<select name="interest" lay-filter="selectType">
					<option value="">全部</option>
					<option value="0">图片</option>
					<option value="1">视频</option>
					<option value="2">音频</option>
				</select>
			</div>
		</div>
		<table id="adList" lay-filter="adList"></table>

		<!--操作-->
		<script type="text/html" id="publish">
		<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
		<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
	</script>
	</form>
	<script>
	layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
	
	    if($("#compId").val()==0){
	    	$(".selectComp").show();
	    }else{
	    	$(".selectComp").hide();
	    }
	    	//公司
	     form.on("select(selectComp)",function(data){
	    	 id = data.value;
	 		//执行重载
    	    table.reload('adListTable', {
				  url: '/publishFile/list'
				  ,page: {
			          curr: 1 //重新从第 1 页开始
			        }
				  ,where: {
					  compIdParam: id
				  } 
			});
	    })
	
	     form.on("select(selectType)",function(data){
	    	 id = data.value;
	 		//执行重载
    	    table.reload('adListTable', {
				  url: '/publishFile/list'
				  ,page: {
			          curr: 1 //重新从第 1 页开始
			        }
				  ,where: {
					type: id
				  } 
			});
	    })
	
	    var tableIns = table.render({
	        elem: '#adList',
	        url : '/publishFile/list',
	        page : true,
	        height : "full-145",
	        limit : 20,
	        limits : [10,15,20,25],
	        id : "adListTable",
	        cols : [[
	            {field: 'name', title: '资源名称',  align:'center', sort: true},
	            {field: 'des', title: '描述', align:'center', sort: true},
	            {field: 'compName', title: '所属公司', align:'center', sort: true},
	            {field: 'type', title: '类型', align:'center' , sort: true, width :133,templet:function(f){
	            	if(f.type == 0){
	            		return "<span>图片</span>"
	            	}else if(f.type == 1){
	            		return "<span>视频</span>"
	            	}else if(f.type == 2){
	            		return "<span>音频</span>"
	            	}
	            }},
	            {field: 'url', title: '地址', align:'center',templet:function(f){
	            	var urlstr=f.url;
	            	return '<a href="'+urlstr+'" target="target" >'+urlstr+'</a>'
	            } },
	            {field: 'creatTime', title: '上传时间', align:'center'},
	            {title: '操作', width:90, templet:'#publish',fixed:"right",align:"center",width :150}
	        ]]
	    });
	
	    $(".addbtn").click(function(){
	    	var index = layui.layer.open({
	            title : "新增广告资源",
	            type : 2,
	            content : "/publishFile/add",
	            success : function(layero, index){
	                setTimeout(function(){
	                    layui.layer.tips('点击此处返回广告列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500)
	            }
	        })
	        layui.layer.full(index);
	        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
	        $(window).on("resize",function(){
	            layui.layer.full(index);
	        })
	    })
	    
	    
	    function addAd(edit){
	        var index = layui.layer.open({
	            title : "广告编辑",
	            type : 2,
	            content : "/publishFile/edit?fileId="+edit.fileId,
	            success : function(layero, index){
	                var body = layui.layer.getChildFrame('body', index);
	                if(edit){
	                	body.find(".name").val(edit.name);
	                	body.find(".fileId").val(edit.fileId);
	                    body.find(".url").val(edit.url);
	                    body.find(".des").val(edit.des);
	                    form.render();
	                }
	            }
	        })
	        layui.layer.full(index);
	        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
	        $(window).on("resize",function(){
	            layui.layer.full(index);
	        })
	    }
	    //列表操作
	    table.on('tool(adList)', function(obj){
	        var layEvent = obj.event,
	            data = obj.data;
	        if(layEvent === 'edit'){ //编辑
	            addAd(data);
	        } else if(layEvent === 'del'){ //删除
	            layer.confirm('确定删除此广告？',{icon:3, title:'提示信息'},function(index){
	            	$.post("/publishFile/deleteFile?fileId="+data.fileId,function(rest){
	            		layer.msg(rest.message);
	            	})
	                tableIns.reload();
	                layer.close(index);
	            });
	        }
	    });
	
	})
</script>
</body>
</html>